package tt.dz.biz.dao.common;

import java.sql.Time;
import java.util.Date;
import java.util.List;
import java.util.Map;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;
import tt.dz.biz.dao.BaseDAO;
import tt.dz.common.LogUtil;

@Repository("rpcAccessWhiteListDAO")
public class RpcAccessWhiteListDAO
  extends BaseDAO
{
  public static final String QUERY_MS_RPC_ACCESS_WHITELIST = "select  ip_address,host_name,access_server_name,control_type,valid_time_start,valid_time_end,valid_date_start,valid_date_end,remark  from ms_rpc_access_whiteList ";
  
  public boolean CheckClientIP(String ip)
  {
    MapSqlParameterSource parameters = new MapSqlParameterSource();
    parameters.addValue("accessServerName", "MS-SERVER");
    List<Map<String, Object>> ipList = queryForList("select  ip_address,host_name,access_server_name,control_type,valid_time_start,valid_time_end,valid_date_start,valid_date_end,remark  from ms_rpc_access_whiteList  where access_server_name=:accessServerName", parameters);
    if (!CollectionUtils.isEmpty(ipList))
    {
      boolean isAllPass = false;
      Map<String, Object> currentRow = null;
      for (Map<String, Object> ipRow : ipList)
      {
        String ipAddress = (String)ipRow.get("ipAddress");
        if ("*".equals(ipAddress))
        {
          isAllPass = true;
          break;
        }
        if (ip.trim().equals(ipAddress.trim())) {
          currentRow = ipRow;
        }
      }
      if (isAllPass)
      {
        LogUtil.debug("本服务器配置规则针对所有访问全部通过");
        return true;
      }
      if (!CollectionUtils.isEmpty(currentRow))
      {
        Integer controlType = (Integer)currentRow.get("controlType");
        boolean isPass = false;
        Time validTimeStart = (Time)currentRow.get("validTimeStart");
        Time validTimeEnd = (Time)currentRow.get("validTimeEnd");
        Date validDateStart = (Date)currentRow.get("validDateStart");
        Date validDateEnd = (Date)currentRow.get("validDateEnd");
        Date nowDate = new Date();
        switch (controlType.intValue())
        {
        case 0: 
          LogUtil.debug("ip:" + ip + "访问权限无时间段控制");
          isPass = true;
          break;
        case 1: 
          LogUtil.debug("ip:" + ip + "访问权限为限制时间段访问;开始时间:" + validTimeStart + ",结束时间:" + validTimeEnd);
          if ((validTimeStart != null) && (validTimeEnd != null))
          {
            Date _validTimeStart = new Date(nowDate.getYear(), nowDate.getMonth(), nowDate.getDate(), validTimeStart.getHours(), validTimeStart.getMinutes(), validTimeStart.getSeconds());
            
            Date _validTimeEnd = new Date(nowDate.getYear(), nowDate.getMonth(), nowDate.getDate(), validTimeEnd.getHours(), validTimeEnd.getMinutes(), validTimeEnd.getSeconds());
            if ((_validTimeStart.before(nowDate)) && (nowDate.before(_validTimeEnd))) {
              isPass = true;
            } else {
              isPass = false;
            }
          }
          else
          {
            isPass = false;
          }
          break;
        case 2: 
          LogUtil.debug("ip:" + ip + "访问权限为限制日期访问;开始日期:" + validDateStart + ",结束时间:" + validDateEnd);
          if ((validDateStart != null) && (validDateEnd != null))
          {
            Date _validDateStart = new Date(validDateStart.getYear(), validDateStart.getMonth(), validDateStart.getDate(), 0, 0, 0);
            
            Date _validDateEnd = new Date(validDateEnd.getYear(), validDateEnd.getMonth(), validDateEnd.getDate(), 23, 59, 59);
            if ((_validDateStart.before(nowDate)) && (nowDate.before(_validDateEnd))) {
              isPass = true;
            } else {
              isPass = false;
            }
          }
          else
          {
            isPass = false;
          }
          break;
        case 3: 
          LogUtil.debug("ip:" + ip + "访问权限为限制日期并限制时间段访问;开始日期:" + validDateStart + ",结束时间:" + validDateEnd + ";开始时间:" + validTimeStart + ",结束时间:" + validTimeEnd);
          if ((validDateStart != null) && (validDateEnd != null) && (validTimeStart != null) && (validTimeEnd != null))
          {
            Date _validDateTimeStart = new Date(validDateStart.getYear(), validDateStart.getMonth(), validDateStart.getDate(), validTimeStart.getHours(), validTimeStart.getMinutes(), validTimeStart.getSeconds());
            

            Date _validDateTimeEnd = new Date(validDateEnd.getYear(), validDateEnd.getMonth(), validDateEnd.getDate(), validTimeEnd.getHours(), validTimeEnd.getMinutes(), validTimeEnd.getSeconds());
            if ((_validDateTimeStart.before(nowDate)) && (nowDate.before(_validDateTimeEnd))) {
              isPass = true;
            } else {
              isPass = false;
            }
          }
          else
          {
            isPass = false;
          }
          break;
        }
        return isPass;
      }
      return false;
    }
    LogUtil.debug("ip:" + ip + "未配置本服务器的访问规则");
    return true;
  }
}
